package com.yifeng.repo.base.security.console.impl;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yifeng.base.account.api.AccountPermissionRestService;
import com.yifeng.base.account.api.dto.AdmOrgCoreInfoDto;
import com.yifeng.base.account.api.dto.AdmOrgUserRelation;
import com.yifeng.base.account.api.dto.ButtonDto;
import com.yifeng.base.account.api.param.SecuredOrgParam;
import com.yifeng.repo.base.dto.PermissionDto;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;
import com.yifeng.repo.base.utils.converter.JacksonHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2024/2/28.
 */
public class BusinessPermissionServiceImpl extends CommonProxy implements AccountPermissionRestService {
    private final String mapping;

    public BusinessPermissionServiceImpl(ConsoleParam consoleParam) {
        super(consoleParam);
        this.mapping = "/yfcloud-user/rest/accountPermission";
    }

    @Override
    public List<String> listPublicKey() {
        String url = consoleParam.getUrl() + mapping + "/listPublicKey";
        return this.doListCallByRpc(url, String.class);
    }

    @Override
    public PermissionDto getPermission(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/getPermission";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, PermissionDto.class);
    }

    @Override
    public boolean checkApiSecured(String userCode, String uri) {
        String url = consoleParam.getUrl() + mapping + "/checkApiSecured";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("userCode", userCode);
        params.put("uri", uri);
        return this.doActionCallByRpc(url, params.toString(), Boolean.class);
    }

    @Override
    public boolean checkButtonSecured(String userCode, String button) {
        String url = consoleParam.getUrl() + mapping + "/checkButtonSecured";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("userCode", userCode);
        params.put("button", button);
        return this.doActionCallByRpc(url, params.toString(), Boolean.class);
    }

    @Override
    public List<ButtonDto> listButtonSecured(String userCode, String menu) {
        String url = consoleParam.getUrl() + mapping + "/listButtonSecured";
        Map<String, String> params = new HashMap<>(2);
        params.put("userCode", userCode);
        params.put("menu", menu);
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, ButtonDto.class);
    }

    @Override
    public boolean checkStoreSecured(String userCode, List<String> storeCodes) {
        String url = consoleParam.getUrl() + mapping + "/checkStoreSecured";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("userCode", userCode);
        params.put("storeCodes", JacksonHelper.toJson(storeCodes));
        return this.doActionCallByRpc(url, params.toString(), Boolean.class);
    }

    @Override
    public List<AdmOrgCoreInfoDto> listOrgPathSecured(String userCode, Boolean enable, Boolean alliance) {
        String url = consoleParam.getUrl() + mapping + "/listOrgPathSecured";
        Map<String, String> params = new HashMap<>();
        params.put("userCode", userCode);
        params.put("enable", enable != null ? enable.toString() : null);
        params.put("alliance", alliance != null ? alliance.toString() : null);
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgCoreInfoDto.class);
    }

    @Override
    public List<AdmOrgCoreInfoDto> listOrgSecured(SecuredOrgParam securedOrgParam) {
        String url = consoleParam.getUrl() + mapping + "/listOrgSecured";
        Map<String, String> params = new HashMap<>();
        params.put("userCode", securedOrgParam.getUserCode());
        params.put("alliance", securedOrgParam.getAlliance() != null ? securedOrgParam.getAlliance().toString() : null);
        params.put("enable", securedOrgParam.getEnable() != null ? securedOrgParam.getEnable().toString() : null);
        params.put("condition", securedOrgParam.getCondition());
        params.put("pageNo", securedOrgParam.getPageNo() != null ? securedOrgParam.getPageNo().toString() : null);
        params.put("pageSize", securedOrgParam.getPageSize() != null ? securedOrgParam.getPageSize().toString() : null);
        params.put("needOrgPath", securedOrgParam.getNeedOrgPath() != null ? securedOrgParam.getNeedOrgPath().toString() : null);
        String orgCodesQueryParameters = buildCollectionQueryParameters("orgCodes", securedOrgParam.getOrgCodes(), true);
        String orgRanksQueryParameters = buildCollectionQueryParameters("orgRanks", securedOrgParam.getOrgRanks(), true);
        String queryParameters = buildQueryParameters(params) + orgCodesQueryParameters + orgRanksQueryParameters;
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgCoreInfoDto.class);
    }

    @Override
    public long countOrgSecured(SecuredOrgParam securedOrgParam) {
        String url = consoleParam.getUrl() + mapping + "/countOrgSecured";
        Map<String, String> params = new HashMap<>();
        params.put("userCode", securedOrgParam.getUserCode());
        params.put("alliance", securedOrgParam.getAlliance() != null ? securedOrgParam.getAlliance().toString() : null);
        params.put("enable", securedOrgParam.getEnable() != null ? securedOrgParam.getEnable().toString() : null);
        params.put("condition", securedOrgParam.getCondition());
        params.put("pageNo", securedOrgParam.getPageNo() != null ? securedOrgParam.getPageNo().toString() : null);
        params.put("pageSize", securedOrgParam.getPageSize() != null ? securedOrgParam.getPageSize().toString() : null);
        params.put("needOrgPath", securedOrgParam.getNeedOrgPath() != null ? securedOrgParam.getNeedOrgPath().toString() : null);
        String orgCodesQueryParameters = buildCollectionQueryParameters("orgCodes", securedOrgParam.getOrgCodes(), true);
        String orgRanksQueryParameters = buildCollectionQueryParameters("orgRanks", securedOrgParam.getOrgRanks(), true);
        String queryParameters = buildQueryParameters(params) + orgCodesQueryParameters + orgRanksQueryParameters;
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, Long.class);
    }

    @Override
    public List<AdmOrgCoreInfoDto> listAndCheckOrgSecured(String userCode, List<String> orgCodes) {
        String url = consoleParam.getUrl() + mapping + "/listAndCheckOrgSecured";
        String userCodeParameters = buildSingleQueryParameters("userCode", userCode);
        String orgCodesParameters = buildCollectionQueryParameters("orgCodes", orgCodes, true);
        String queryParameters = userCodeParameters + orgCodesParameters;
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgCoreInfoDto.class);
    }

    @Override
    public AdmOrgUserRelation getOrgRelationSecured(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/getOrgRelationSecured";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgUserRelation.class);
    }
}
